{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "f8f5a8b1-786d-4234-ada1-bfa94dd31343",
   "metadata": {},
   "source": [
    "## 一、索引对象"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "dd1b4def-d2a6-4317-9487-b8275bd81014",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-20T12:11:56.312690Z",
     "iopub.status.busy": "2022-04-20T12:11:56.312414Z",
     "iopub.status.idle": "2022-04-20T12:11:56.561860Z",
     "shell.execute_reply": "2022-04-20T12:11:56.561223Z",
     "shell.execute_reply.started": "2022-04-20T12:11:56.312603Z"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "# 导入包\n",
    "import numpy as np\n",
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b822538a-78c1-4b31-adda-4bcd7fae12dc",
   "metadata": {},
   "source": [
    "## 小结\n",
    "1. 索引可以系统自动生成，也可以手动指定\n",
    "2. 行索引和列索引都是索引对象\n",
    "3. 索引一旦指定，但就不可更改，只能重建\n",
    "4. 索引有多重类型"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bc7ecba6-0ebc-4048-93b9-44e636c1f041",
   "metadata": {},
   "source": [
    "### 1.Series和DataFrame的索引对象"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "fa0daa81-e843-4268-a45f-4d16400567a6",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-20T12:11:56.564372Z",
     "iopub.status.busy": "2022-04-20T12:11:56.564143Z",
     "iopub.status.idle": "2022-04-20T12:11:56.574553Z",
     "shell.execute_reply": "2022-04-20T12:11:56.573843Z",
     "shell.execute_reply.started": "2022-04-20T12:11:56.564334Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    0\n",
       "1    1\n",
       "2    2\n",
       "3    3\n",
       "4    4\n",
       "dtype: int64"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 创建Series对象，不指定索引\n",
    "pds = pd.Series(np.arange(5))\n",
    "pds"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "c52f2b6b-71c7-44e4-9b89-cae3c4661301",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-20T12:11:56.575775Z",
     "iopub.status.busy": "2022-04-20T12:11:56.575390Z",
     "iopub.status.idle": "2022-04-20T12:11:56.586230Z",
     "shell.execute_reply": "2022-04-20T12:11:56.585401Z",
     "shell.execute_reply.started": "2022-04-20T12:11:56.575741Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>0</th>\n",
       "      <th>1</th>\n",
       "      <th>2</th>\n",
       "      <th>3</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>4</td>\n",
       "      <td>5</td>\n",
       "      <td>6</td>\n",
       "      <td>7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>8</td>\n",
       "      <td>9</td>\n",
       "      <td>10</td>\n",
       "      <td>11</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>12</td>\n",
       "      <td>13</td>\n",
       "      <td>14</td>\n",
       "      <td>15</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    0   1   2   3\n",
       "0   0   1   2   3\n",
       "1   4   5   6   7\n",
       "2   8   9  10  11\n",
       "3  12  13  14  15"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 创建DataFrame对象，不指定索引\n",
    "df = pd.DataFrame(np.arange(16).reshape(4, 4))\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "d15009c6-9b42-4de6-8a29-cb76650b0be4",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-20T12:11:56.587682Z",
     "iopub.status.busy": "2022-04-20T12:11:56.587409Z",
     "iopub.status.idle": "2022-04-20T12:11:56.594673Z",
     "shell.execute_reply": "2022-04-20T12:11:56.593977Z",
     "shell.execute_reply.started": "2022-04-20T12:11:56.587650Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "a    0\n",
       "b    1\n",
       "c    2\n",
       "d    3\n",
       "e    4\n",
       "dtype: int64"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 创建Series对象，并指定索引\n",
    "pds = pd.Series(np.arange(5), index=[\"a\", \"b\", \"c\", \"d\", \"e\"])\n",
    "pds"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "6a5bf2a7-6b2b-430c-8512-5ae65489b576",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-20T12:11:56.597048Z",
     "iopub.status.busy": "2022-04-20T12:11:56.596155Z",
     "iopub.status.idle": "2022-04-20T12:11:56.605503Z",
     "shell.execute_reply": "2022-04-20T12:11:56.605069Z",
     "shell.execute_reply.started": "2022-04-20T12:11:56.596987Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>a</th>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>b</th>\n",
       "      <td>4</td>\n",
       "      <td>5</td>\n",
       "      <td>6</td>\n",
       "      <td>7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>c</th>\n",
       "      <td>8</td>\n",
       "      <td>9</td>\n",
       "      <td>10</td>\n",
       "      <td>11</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>d</th>\n",
       "      <td>12</td>\n",
       "      <td>13</td>\n",
       "      <td>14</td>\n",
       "      <td>15</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    A   B   C   D\n",
       "a   0   1   2   3\n",
       "b   4   5   6   7\n",
       "c   8   9  10  11\n",
       "d  12  13  14  15"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 创建DataFrame对象，并指定索引\n",
    "df = pd.DataFrame(\n",
    "    np.arange(16).reshape(4, 4),\n",
    "    index=[\"a\", \"b\", \"c\", \"d\"],\n",
    "    columns=[\"A\", \"B\", \"C\", \"D\"],\n",
    ")\n",
    "df"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "caf3dfea-f7f5-4423-ad63-598abea8c0e1",
   "metadata": {},
   "source": [
    "### 2.索引对象不可更改"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "9f7bf96e-98a2-470c-9be0-a38ac5e462d9",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-20T12:11:56.606547Z",
     "iopub.status.busy": "2022-04-20T12:11:56.606233Z",
     "iopub.status.idle": "2022-04-20T12:11:56.610443Z",
     "shell.execute_reply": "2022-04-20T12:11:56.609870Z",
     "shell.execute_reply.started": "2022-04-20T12:11:56.606528Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.indexes.base.Index'>\n",
      "<class 'pandas.core.indexes.base.Index'>\n",
      "<class 'pandas.core.indexes.base.Index'>\n"
     ]
    }
   ],
   "source": [
    "# 查看索引类型\n",
    "print(type(pds.index))\n",
    "print(type(df.index))\n",
    "print(type(df.columns))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "4b2bfe52-c6b2-4e76-8dea-1e7839d5879a",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-20T12:11:56.613575Z",
     "iopub.status.busy": "2022-04-20T12:11:56.613347Z",
     "iopub.status.idle": "2022-04-20T12:11:56.617740Z",
     "shell.execute_reply": "2022-04-20T12:11:56.617137Z",
     "shell.execute_reply.started": "2022-04-20T12:11:56.613552Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n"
     ]
    }
   ],
   "source": [
    "print(pds[0])\n",
    "pds[0] = 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "818802e1-df92-45b2-b9a2-5c260eff3caa",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-20T12:11:56.619130Z",
     "iopub.status.busy": "2022-04-20T12:11:56.618790Z",
     "iopub.status.idle": "2022-04-20T12:11:57.614489Z",
     "shell.execute_reply": "2022-04-20T12:11:57.613500Z",
     "shell.execute_reply.started": "2022-04-20T12:11:56.619105Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a\n"
     ]
    },
    {
     "ename": "TypeError",
     "evalue": "Index does not support mutable operations",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "Input \u001b[0;32mIn [8]\u001b[0m, in \u001b[0;36m<cell line: 3>\u001b[0;34m()\u001b[0m\n\u001b[1;32m      1\u001b[0m \u001b[38;5;66;03m# 修改索引值\u001b[39;00m\n\u001b[1;32m      2\u001b[0m \u001b[38;5;28mprint\u001b[39m(pds\u001b[38;5;241m.\u001b[39mindex[\u001b[38;5;241m0\u001b[39m])\n\u001b[0;32m----> 3\u001b[0m pds\u001b[38;5;241m.\u001b[39mindex[\u001b[38;5;241m0\u001b[39m] \u001b[38;5;241m=\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124ma\u001b[39m\u001b[38;5;124m\"\u001b[39m\n",
      "File \u001b[0;32m/opt/miniconda3/envs/jpt/lib/python3.10/site-packages/pandas/core/indexes/base.py:5021\u001b[0m, in \u001b[0;36mIndex.__setitem__\u001b[0;34m(self, key, value)\u001b[0m\n\u001b[1;32m   5019\u001b[0m \u001b[38;5;129m@final\u001b[39m\n\u001b[1;32m   5020\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21m__setitem__\u001b[39m(\u001b[38;5;28mself\u001b[39m, key, value):\n\u001b[0;32m-> 5021\u001b[0m     \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mTypeError\u001b[39;00m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mIndex does not support mutable operations\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n",
      "\u001b[0;31mTypeError\u001b[0m: Index does not support mutable operations"
     ]
    }
   ],
   "source": [
    "# 修改索引值\n",
    "print(pds.index[0])\n",
    "pds.index[0] = \"a\""
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4c0d6f6f-a4e9-45f4-a6c0-aee6a22f17e1",
   "metadata": {},
   "source": [
    "## 二、重新索引"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "2592df6b-d455-43b8-acda-050ddd31e96f",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-20T12:44:11.752649Z",
     "iopub.status.busy": "2022-04-20T12:44:11.752403Z",
     "iopub.status.idle": "2022-04-20T12:44:11.762411Z",
     "shell.execute_reply": "2022-04-20T12:44:11.761321Z",
     "shell.execute_reply.started": "2022-04-20T12:44:11.752623Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1   NaN\n",
       "2   NaN\n",
       "3   NaN\n",
       "4   NaN\n",
       "5   NaN\n",
       "6   NaN\n",
       "dtype: float64"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# reindex 创建一个符合新索引的新Series对象\n",
    "pds2 = pds.reindex([1, 2, 3, 4, 5, 6])\n",
    "pds2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "7106e23d-35f4-4504-929b-894d482cd3ee",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-20T13:19:05.128075Z",
     "iopub.status.busy": "2022-04-20T13:19:05.127840Z",
     "iopub.status.idle": "2022-04-20T13:19:05.133811Z",
     "shell.execute_reply": "2022-04-20T13:19:05.132407Z",
     "shell.execute_reply.started": "2022-04-20T13:19:05.128054Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "a    2\n",
       "b    1\n",
       "c    2\n",
       "d    3\n",
       "e    4\n",
       "dtype: int64"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pds"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "a82bbe49-6d2c-45a1-898e-95ce3d2ae385",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-20T12:59:04.854098Z",
     "iopub.status.busy": "2022-04-20T12:59:04.853854Z",
     "iopub.status.idle": "2022-04-20T12:59:04.860199Z",
     "shell.execute_reply": "2022-04-20T12:59:04.859617Z",
     "shell.execute_reply.started": "2022-04-20T12:59:04.854074Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "a    2.0\n",
       "b    1.0\n",
       "c    2.0\n",
       "d    3.0\n",
       "e    4.0\n",
       "f    NaN\n",
       "dtype: float64"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# reindex 创建一个符合新索引的新Series对象\n",
    "pds2 = pds.reindex([\"a\", \"b\", \"c\", \"d\", \"e\", \"f\"])\n",
    "pds2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "45207699-4006-4353-b571-066e0a202aa6",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-20T13:16:12.934110Z",
     "iopub.status.busy": "2022-04-20T13:16:12.933862Z",
     "iopub.status.idle": "2022-04-20T13:16:12.941066Z",
     "shell.execute_reply": "2022-04-20T13:16:12.939898Z",
     "shell.execute_reply.started": "2022-04-20T13:16:12.934085Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "c    2\n",
       "b    1\n",
       "d    3\n",
       "a    2\n",
       "dtype: int64"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# reindex 创建一个符合新索引的新Series对象\n",
    "pds.reindex([\"c\", \"b\", \"d\", \"a\"])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "aa46dbe8-f725-4cb0-a3dc-fec34c604fad",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-21T00:58:34.038645Z",
     "iopub.status.busy": "2022-04-21T00:58:34.038208Z",
     "iopub.status.idle": "2022-04-21T00:58:34.048921Z",
     "shell.execute_reply": "2022-04-21T00:58:34.048210Z",
     "shell.execute_reply.started": "2022-04-21T00:58:34.038602Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>d</th>\n",
       "      <td>12</td>\n",
       "      <td>13</td>\n",
       "      <td>14</td>\n",
       "      <td>15</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>a</th>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    A   B   C   D\n",
       "d  12  13  14  15\n",
       "a   0   1   2   3"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# reindex 创建一个符合新行索引的新DataFrame对象\n",
    "df.reindex([\"d\", \"a\"])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "9b2ae71c-e903-43bc-a3f0-566868b2fc3f",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-21T01:04:38.318828Z",
     "iopub.status.busy": "2022-04-21T01:04:38.318607Z",
     "iopub.status.idle": "2022-04-21T01:04:38.329867Z",
     "shell.execute_reply": "2022-04-21T01:04:38.329297Z",
     "shell.execute_reply.started": "2022-04-21T01:04:38.318803Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>C</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>a</th>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>b</th>\n",
       "      <td>4</td>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>c</th>\n",
       "      <td>8</td>\n",
       "      <td>10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>d</th>\n",
       "      <td>12</td>\n",
       "      <td>14</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    A   C\n",
       "a   0   2\n",
       "b   4   6\n",
       "c   8  10\n",
       "d  12  14"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# reindex 创建一个符合新列索引的新DataFrame对象\n",
    "df.reindex(columns=[\"A\", \"C\"])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "650311bd-d71a-4629-ab34-8fc7450a5d35",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-21T01:22:11.215236Z",
     "iopub.status.busy": "2022-04-21T01:22:11.214970Z",
     "iopub.status.idle": "2022-04-21T01:22:11.224551Z",
     "shell.execute_reply": "2022-04-21T01:22:11.223804Z",
     "shell.execute_reply.started": "2022-04-21T01:22:11.215209Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>C</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>a</th>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>c</th>\n",
       "      <td>8</td>\n",
       "      <td>10</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   A   C\n",
       "a  0   2\n",
       "c  8  10"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# reindex 创建一个符合新行索引和列索引的新DataFrame对象\n",
    "df.reindex(index=[\"a\", \"c\"], columns=[\"A\", \"C\"])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ce9726df-0f38-42c8-8871-882036cd149e",
   "metadata": {},
   "source": [
    "## 三、增、删、改、查"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b667f60c-9347-4254-847f-026edfe61449",
   "metadata": {},
   "source": [
    "### 小结\n",
    "1. 增: Series和DataFrame的增操作\n",
    "    1. Series对象[“索引”] = 数据\n",
    "    2. DataFrame对象[“列索引”] = 数据\n",
    "    3. DataFrame对象.insert( 位置, 标签, 数据)\n",
    "    4. 不直接操作原数据使用concat函数\n",
    "2. 删: Series和DataFrame的删操作\n",
    "    1. del(Series对象[“索引”] )\n",
    "    2. del(DataFrame对象[“列索引”] )\n",
    "    3. Series对象.drop(“索引”/[“索引i”, “索引j”, …])\n",
    "    4. DataFrame对象.drop(“行索引”//[“索引i”, “索引j”, …])\n",
    "    5. DataFrame对象.drop(“行索引”, axis=1/”columns”)\n",
    "    6. 对象.drop(“索引”, inplace=True)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a8450990-ab5c-4976-a3fe-6f11e84b699b",
   "metadata": {},
   "source": [
    "### 3.1 增"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5e027056-64ce-480a-a104-428a8b37dfb2",
   "metadata": {},
   "source": [
    "#### 3.1.1 Series的增操作"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "5ca95c0b-6315-457f-ba96-7494152e8ec3",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-22T08:54:37.836697Z",
     "iopub.status.busy": "2022-04-22T08:54:37.836355Z",
     "iopub.status.idle": "2022-04-22T08:54:37.850460Z",
     "shell.execute_reply": "2022-04-22T08:54:37.849667Z",
     "shell.execute_reply.started": "2022-04-22T08:54:37.836652Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "a    2\n",
       "b    1\n",
       "c    2\n",
       "d    3\n",
       "e    4\n",
       "dtype: int64"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 查看pds\n",
    "pds"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "8405fe8c-233c-4110-a38c-e73eb2eb16e9",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-22T09:00:26.549206Z",
     "iopub.status.busy": "2022-04-22T09:00:26.548949Z",
     "iopub.status.idle": "2022-04-22T09:00:26.556197Z",
     "shell.execute_reply": "2022-04-22T09:00:26.555364Z",
     "shell.execute_reply.started": "2022-04-22T09:00:26.549180Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "a    2\n",
       "b    1\n",
       "c    2\n",
       "d    3\n",
       "e    4\n",
       "g    8\n",
       "dtype: int64"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 给pds增加数据\n",
    "pds[\"g\"] = 8\n",
    "pds"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "f2cf98b6-5b2d-4a42-91b7-72c6fdabe83c",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-23T00:18:40.897220Z",
     "iopub.status.busy": "2022-04-23T00:18:40.896266Z",
     "iopub.status.idle": "2022-04-23T00:18:40.994683Z",
     "shell.execute_reply": "2022-04-23T00:18:40.991662Z",
     "shell.execute_reply.started": "2022-04-23T00:18:40.896611Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "a    2\n",
       "b    1\n",
       "c    2\n",
       "d    3\n",
       "e    4\n",
       "g    8\n",
       "dtype: int64"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 查看pds\n",
    "pds"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "082f9cd7-c2e8-44fc-b101-8a91a4fc70c3",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-23T08:48:05.601207Z",
     "iopub.status.busy": "2022-04-23T08:48:05.600674Z",
     "iopub.status.idle": "2022-04-23T08:48:05.610278Z",
     "shell.execute_reply": "2022-04-23T08:48:05.609288Z",
     "shell.execute_reply.started": "2022-04-23T08:48:05.601178Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "f    20\n",
      "dtype: int64\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "a     2\n",
       "b     1\n",
       "c     2\n",
       "d     3\n",
       "e     4\n",
       "g     8\n",
       "f    20\n",
       "dtype: int64"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 使用concat拼接Series对象\n",
    "s1 = pd.Series({\"f\": 20})\n",
    "print(s1)\n",
    "pds3 = pd.concat([pds, s1])\n",
    "pds3"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "77cd1620-00aa-46a6-ac4f-6088e23789c7",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-23T08:44:07.781027Z",
     "iopub.status.busy": "2022-04-23T08:44:07.780781Z",
     "iopub.status.idle": "2022-04-23T08:44:07.788916Z",
     "shell.execute_reply": "2022-04-23T08:44:07.787854Z",
     "shell.execute_reply.started": "2022-04-23T08:44:07.781002Z"
    },
    "tags": []
   },
   "source": [
    "#### 3.1.2 DataFrame的增操作"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "dd204c1c-04b2-40db-a81b-c12e0a9a411e",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-23T09:06:48.420479Z",
     "iopub.status.busy": "2022-04-23T09:06:48.420238Z",
     "iopub.status.idle": "2022-04-23T09:06:48.428257Z",
     "shell.execute_reply": "2022-04-23T09:06:48.427512Z",
     "shell.execute_reply.started": "2022-04-23T09:06:48.420454Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>a</th>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>b</th>\n",
       "      <td>4</td>\n",
       "      <td>5</td>\n",
       "      <td>6</td>\n",
       "      <td>7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>c</th>\n",
       "      <td>8</td>\n",
       "      <td>9</td>\n",
       "      <td>10</td>\n",
       "      <td>11</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>d</th>\n",
       "      <td>12</td>\n",
       "      <td>13</td>\n",
       "      <td>14</td>\n",
       "      <td>15</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    A   B   C   D\n",
       "a   0   1   2   3\n",
       "b   4   5   6   7\n",
       "c   8   9  10  11\n",
       "d  12  13  14  15"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 查看df\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "588e1db8-b5b0-4884-a835-0b540fc03301",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-23T09:10:58.560976Z",
     "iopub.status.busy": "2022-04-23T09:10:58.560730Z",
     "iopub.status.idle": "2022-04-23T09:10:58.570392Z",
     "shell.execute_reply": "2022-04-23T09:10:58.569680Z",
     "shell.execute_reply.started": "2022-04-23T09:10:58.560951Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "      <th>7</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>a</th>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "      <td>20</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>b</th>\n",
       "      <td>4</td>\n",
       "      <td>5</td>\n",
       "      <td>6</td>\n",
       "      <td>7</td>\n",
       "      <td>20</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>c</th>\n",
       "      <td>8</td>\n",
       "      <td>9</td>\n",
       "      <td>10</td>\n",
       "      <td>11</td>\n",
       "      <td>20</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>d</th>\n",
       "      <td>12</td>\n",
       "      <td>13</td>\n",
       "      <td>14</td>\n",
       "      <td>15</td>\n",
       "      <td>20</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    A   B   C   D   7\n",
       "a   0   1   2   3  20\n",
       "b   4   5   6   7  20\n",
       "c   8   9  10  11  20\n",
       "d  12  13  14  15  20"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 给df增加列数据\n",
    "df[7] = 20\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "e65edf90-36c7-4121-bc0b-5132301588d5",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-23T09:19:46.537214Z",
     "iopub.status.busy": "2022-04-23T09:19:46.536962Z",
     "iopub.status.idle": "2022-04-23T09:19:46.546093Z",
     "shell.execute_reply": "2022-04-23T09:19:46.545142Z",
     "shell.execute_reply.started": "2022-04-23T09:19:46.537189Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "      <th>7</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>a</th>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>b</th>\n",
       "      <td>4</td>\n",
       "      <td>5</td>\n",
       "      <td>6</td>\n",
       "      <td>7</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>c</th>\n",
       "      <td>8</td>\n",
       "      <td>9</td>\n",
       "      <td>10</td>\n",
       "      <td>11</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>d</th>\n",
       "      <td>12</td>\n",
       "      <td>13</td>\n",
       "      <td>14</td>\n",
       "      <td>15</td>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    A   B   C   D  7\n",
       "a   0   1   2   3  3\n",
       "b   4   5   6   7  4\n",
       "c   8   9  10  11  5\n",
       "d  12  13  14  15  6"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 给df增加列数据\n",
    "df[7] = [3, 4, 5, 6]\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "aec6d911-887c-42a7-ade9-28390f76a44c",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-23T09:28:35.611568Z",
     "iopub.status.busy": "2022-04-23T09:28:35.611128Z",
     "iopub.status.idle": "2022-04-23T09:28:36.485172Z",
     "shell.execute_reply": "2022-04-23T09:28:36.484121Z",
     "shell.execute_reply.started": "2022-04-23T09:28:35.611543Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "ename": "ValueError",
     "evalue": "cannot insert H, already exists",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mValueError\u001b[0m                                Traceback (most recent call last)",
      "Input \u001b[0;32mIn [41]\u001b[0m, in \u001b[0;36m<cell line: 1>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[43mdf\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43minsert\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m1\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mH\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;241;43m6\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m7\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m8\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m2\u001b[39;49m\u001b[43m]\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m      2\u001b[0m df\n",
      "File \u001b[0;32m/opt/miniconda3/envs/jpt/lib/python3.10/site-packages/pandas/core/frame.py:4440\u001b[0m, in \u001b[0;36mDataFrame.insert\u001b[0;34m(self, loc, column, value, allow_duplicates)\u001b[0m\n\u001b[1;32m   4434\u001b[0m     \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mValueError\u001b[39;00m(\n\u001b[1;32m   4435\u001b[0m         \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mCannot specify \u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mallow_duplicates=True\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m when \u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m   4436\u001b[0m         \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mself.flags.allows_duplicate_labels\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m is False.\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m   4437\u001b[0m     )\n\u001b[1;32m   4438\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m allow_duplicates \u001b[38;5;129;01mand\u001b[39;00m column \u001b[38;5;129;01min\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mcolumns:\n\u001b[1;32m   4439\u001b[0m     \u001b[38;5;66;03m# Should this be a different kind of error??\u001b[39;00m\n\u001b[0;32m-> 4440\u001b[0m     \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mValueError\u001b[39;00m(\u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mcannot insert \u001b[39m\u001b[38;5;132;01m{\u001b[39;00mcolumn\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m, already exists\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[1;32m   4441\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28misinstance\u001b[39m(loc, \u001b[38;5;28mint\u001b[39m):\n\u001b[1;32m   4442\u001b[0m     \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mTypeError\u001b[39;00m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mloc must be int\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n",
      "\u001b[0;31mValueError\u001b[0m: cannot insert H, already exists"
     ]
    }
   ],
   "source": [
    "# 给df指定位置插入列数据\n",
    "df.insert(1, \"H\", [6, 7, 8, 2])\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "736e9fcd-2b7a-45bb-8205-bfa5674016b0",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-23T09:43:35.772698Z",
     "iopub.status.busy": "2022-04-23T09:43:35.772449Z",
     "iopub.status.idle": "2022-04-23T09:43:35.783986Z",
     "shell.execute_reply": "2022-04-23T09:43:35.782938Z",
     "shell.execute_reply.started": "2022-04-23T09:43:35.772672Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>H</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "      <th>7</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>a</th>\n",
       "      <td>0</td>\n",
       "      <td>6</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>b</th>\n",
       "      <td>4</td>\n",
       "      <td>7</td>\n",
       "      <td>5</td>\n",
       "      <td>6</td>\n",
       "      <td>7</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>c</th>\n",
       "      <td>8</td>\n",
       "      <td>8</td>\n",
       "      <td>9</td>\n",
       "      <td>10</td>\n",
       "      <td>11</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>d</th>\n",
       "      <td>12</td>\n",
       "      <td>2</td>\n",
       "      <td>13</td>\n",
       "      <td>14</td>\n",
       "      <td>15</td>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>f</th>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    A  H   B   C   D  7\n",
       "a   0  6   1   2   3  3\n",
       "b   4  7   5   6   7  4\n",
       "c   8  8   9  10  11  5\n",
       "d  12  2  13  14  15  6\n",
       "f   2  2   2   2   2  2"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 给df增加行数据\n",
    "df.loc[\"f\"] = [2, 2, 2, 2, 2, 2]\n",
    "df"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "17e676f7-1a98-40d8-97a9-9a088155b596",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-24T01:38:22.994116Z",
     "iopub.status.busy": "2022-04-24T01:38:22.993860Z",
     "iopub.status.idle": "2022-04-24T01:38:22.997110Z",
     "shell.execute_reply": "2022-04-24T01:38:22.996508Z",
     "shell.execute_reply.started": "2022-04-24T01:38:22.994089Z"
    }
   },
   "source": [
    "### 3.2 删"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "114ca656-dccd-4be5-a26d-e1f341eb8f77",
   "metadata": {},
   "source": [
    "#### 3.2.1 del函数"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8ddf7528-1a9f-49c2-8f22-b16298366f09",
   "metadata": {},
   "source": [
    "##### 3.2.1.1 Series操作"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "id": "974ee1ab-27c2-4697-9705-4bb4d1179efb",
   "metadata": {
    "collapsed": true,
    "execution": {
     "iopub.execute_input": "2022-04-24T01:41:09.565160Z",
     "iopub.status.busy": "2022-04-24T01:41:09.564911Z",
     "iopub.status.idle": "2022-04-24T01:41:09.570737Z",
     "shell.execute_reply": "2022-04-24T01:41:09.569992Z",
     "shell.execute_reply.started": "2022-04-24T01:41:09.565135Z"
    },
    "jupyter": {
     "outputs_hidden": true
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "a    2\n",
       "b    1\n",
       "c    2\n",
       "d    3\n",
       "e    4\n",
       "g    8\n",
       "dtype: int64"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 查看pds\n",
    "pds"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "id": "d08453ed-9ccb-4697-8991-c3e242410751",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-24T01:45:20.040723Z",
     "iopub.status.busy": "2022-04-24T01:45:20.040464Z",
     "iopub.status.idle": "2022-04-24T01:45:20.046363Z",
     "shell.execute_reply": "2022-04-24T01:45:20.045744Z",
     "shell.execute_reply.started": "2022-04-24T01:45:20.040698Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "b    1\n",
       "c    2\n",
       "d    3\n",
       "e    4\n",
       "g    8\n",
       "dtype: int64"
      ]
     },
     "execution_count": 49,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 用del函数删除索引“a”\n",
    "del pds[\"a\"]\n",
    "pds"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d1ec126d-66ca-4a1b-9c4b-7de5631f696f",
   "metadata": {},
   "source": [
    "##### 3.2.1.2 DataFrame操作"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "id": "7982fbae-1b60-434f-ad71-9a0fa306fd83",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-24T01:50:30.301344Z",
     "iopub.status.busy": "2022-04-24T01:50:30.301097Z",
     "iopub.status.idle": "2022-04-24T01:50:30.310859Z",
     "shell.execute_reply": "2022-04-24T01:50:30.310204Z",
     "shell.execute_reply.started": "2022-04-24T01:50:30.301319Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>H</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "      <th>7</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>a</th>\n",
       "      <td>0</td>\n",
       "      <td>6</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>b</th>\n",
       "      <td>4</td>\n",
       "      <td>7</td>\n",
       "      <td>5</td>\n",
       "      <td>6</td>\n",
       "      <td>7</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>c</th>\n",
       "      <td>8</td>\n",
       "      <td>8</td>\n",
       "      <td>9</td>\n",
       "      <td>10</td>\n",
       "      <td>11</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>d</th>\n",
       "      <td>12</td>\n",
       "      <td>2</td>\n",
       "      <td>13</td>\n",
       "      <td>14</td>\n",
       "      <td>15</td>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>f</th>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    A  H   B   C   D  7\n",
       "a   0  6   1   2   3  3\n",
       "b   4  7   5   6   7  4\n",
       "c   8  8   9  10  11  5\n",
       "d  12  2  13  14  15  6\n",
       "f   2  2   2   2   2  2"
      ]
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 查看df\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "id": "c5d06850-315f-4f56-b04a-283f3603dd02",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-24T01:52:55.702828Z",
     "iopub.status.busy": "2022-04-24T01:52:55.702577Z",
     "iopub.status.idle": "2022-04-24T01:52:55.711055Z",
     "shell.execute_reply": "2022-04-24T01:52:55.710462Z",
     "shell.execute_reply.started": "2022-04-24T01:52:55.702803Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>H</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>a</th>\n",
       "      <td>0</td>\n",
       "      <td>6</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>b</th>\n",
       "      <td>4</td>\n",
       "      <td>7</td>\n",
       "      <td>5</td>\n",
       "      <td>6</td>\n",
       "      <td>7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>c</th>\n",
       "      <td>8</td>\n",
       "      <td>8</td>\n",
       "      <td>9</td>\n",
       "      <td>10</td>\n",
       "      <td>11</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>d</th>\n",
       "      <td>12</td>\n",
       "      <td>2</td>\n",
       "      <td>13</td>\n",
       "      <td>14</td>\n",
       "      <td>15</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>f</th>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    A  H   B   C   D\n",
       "a   0  6   1   2   3\n",
       "b   4  7   5   6   7\n",
       "c   8  8   9  10  11\n",
       "d  12  2  13  14  15\n",
       "f   2  2   2   2   2"
      ]
     },
     "execution_count": 53,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 用del函数删除索引“7”\n",
    "del df[7]\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "id": "d1602979-3f47-4637-9156-7c5a1e24b12d",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-24T01:59:01.248978Z",
     "iopub.status.busy": "2022-04-24T01:59:01.248731Z",
     "iopub.status.idle": "2022-04-24T01:59:01.258595Z",
     "shell.execute_reply": "2022-04-24T01:59:01.257924Z",
     "shell.execute_reply.started": "2022-04-24T01:59:01.248953Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>a</th>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>b</th>\n",
       "      <td>4</td>\n",
       "      <td>5</td>\n",
       "      <td>6</td>\n",
       "      <td>7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>c</th>\n",
       "      <td>8</td>\n",
       "      <td>9</td>\n",
       "      <td>10</td>\n",
       "      <td>11</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>d</th>\n",
       "      <td>12</td>\n",
       "      <td>13</td>\n",
       "      <td>14</td>\n",
       "      <td>15</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>f</th>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    A   B   C   D\n",
       "a   0   1   2   3\n",
       "b   4   5   6   7\n",
       "c   8   9  10  11\n",
       "d  12  13  14  15\n",
       "f   2   2   2   2"
      ]
     },
     "execution_count": 54,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 用del函数删除索引“H”\n",
    "del df[\"H\"]\n",
    "df"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "00514263-80ad-4ef5-9153-74227410ed12",
   "metadata": {},
   "source": [
    "#### 3.2.2 drop操作"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "db7e8f3f-7d06-4f85-9d10-4f220efe65d1",
   "metadata": {},
   "source": [
    "##### 3.2.2.1 Series"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "id": "6e0c6a5b-f563-4c08-9a60-1035911e4fd9",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-24T02:06:46.007518Z",
     "iopub.status.busy": "2022-04-24T02:06:46.007124Z",
     "iopub.status.idle": "2022-04-24T02:06:46.012816Z",
     "shell.execute_reply": "2022-04-24T02:06:46.012237Z",
     "shell.execute_reply.started": "2022-04-24T02:06:46.007493Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "b    1\n",
       "c    2\n",
       "d    3\n",
       "e    4\n",
       "g    8\n",
       "dtype: int64"
      ]
     },
     "execution_count": 55,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 查看pds\n",
    "pds"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "id": "9a0fe912-8214-4900-9d31-742afb62f06a",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-24T02:09:13.481499Z",
     "iopub.status.busy": "2022-04-24T02:09:13.481061Z",
     "iopub.status.idle": "2022-04-24T02:09:13.487175Z",
     "shell.execute_reply": "2022-04-24T02:09:13.486538Z",
     "shell.execute_reply.started": "2022-04-24T02:09:13.481474Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "c    2\n",
       "d    3\n",
       "e    4\n",
       "g    8\n",
       "dtype: int64"
      ]
     },
     "execution_count": 56,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 用drop函数删除索引“b”\n",
    "pds1 = pds.drop(\"b\")\n",
    "pds1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "id": "7c759871-1675-411a-a4af-34342070b5a0",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-24T02:17:37.393790Z",
     "iopub.status.busy": "2022-04-24T02:17:37.393301Z",
     "iopub.status.idle": "2022-04-24T02:17:37.399725Z",
     "shell.execute_reply": "2022-04-24T02:17:37.399172Z",
     "shell.execute_reply.started": "2022-04-24T02:17:37.393764Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "b    1\n",
       "d    3\n",
       "g    8\n",
       "dtype: int64"
      ]
     },
     "execution_count": 57,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 用drop函数删除多个数据\n",
    "pds.drop([\"c\", \"e\"])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dd5fe72e-6207-4aa9-a44d-abdd3c37967c",
   "metadata": {},
   "source": [
    "##### 3.2.2.2 DataFramea"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "id": "607aa3ce-0a4c-4be1-b524-f6698d6e809b",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-24T02:21:39.194752Z",
     "iopub.status.busy": "2022-04-24T02:21:39.194501Z",
     "iopub.status.idle": "2022-04-24T02:21:39.202497Z",
     "shell.execute_reply": "2022-04-24T02:21:39.201636Z",
     "shell.execute_reply.started": "2022-04-24T02:21:39.194726Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>a</th>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>b</th>\n",
       "      <td>4</td>\n",
       "      <td>5</td>\n",
       "      <td>6</td>\n",
       "      <td>7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>c</th>\n",
       "      <td>8</td>\n",
       "      <td>9</td>\n",
       "      <td>10</td>\n",
       "      <td>11</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>d</th>\n",
       "      <td>12</td>\n",
       "      <td>13</td>\n",
       "      <td>14</td>\n",
       "      <td>15</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>f</th>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    A   B   C   D\n",
       "a   0   1   2   3\n",
       "b   4   5   6   7\n",
       "c   8   9  10  11\n",
       "d  12  13  14  15\n",
       "f   2   2   2   2"
      ]
     },
     "execution_count": 58,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 查看df\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "id": "3d1eafcd-14b8-4b4e-9dca-704f4780ed26",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-24T02:23:04.247218Z",
     "iopub.status.busy": "2022-04-24T02:23:04.246968Z",
     "iopub.status.idle": "2022-04-24T02:23:04.255793Z",
     "shell.execute_reply": "2022-04-24T02:23:04.255118Z",
     "shell.execute_reply.started": "2022-04-24T02:23:04.247193Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>b</th>\n",
       "      <td>4</td>\n",
       "      <td>5</td>\n",
       "      <td>6</td>\n",
       "      <td>7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>c</th>\n",
       "      <td>8</td>\n",
       "      <td>9</td>\n",
       "      <td>10</td>\n",
       "      <td>11</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>d</th>\n",
       "      <td>12</td>\n",
       "      <td>13</td>\n",
       "      <td>14</td>\n",
       "      <td>15</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>f</th>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    A   B   C   D\n",
       "b   4   5   6   7\n",
       "c   8   9  10  11\n",
       "d  12  13  14  15\n",
       "f   2   2   2   2"
      ]
     },
     "execution_count": 59,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 用drop函数删除a索引的行数据\n",
    "df.drop(\"a\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "id": "d2619fff-ad7c-4a12-b81b-0ad5a7a7df3c",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-24T02:26:26.236223Z",
     "iopub.status.busy": "2022-04-24T02:26:26.235977Z",
     "iopub.status.idle": "2022-04-24T02:26:26.244412Z",
     "shell.execute_reply": "2022-04-24T02:26:26.243604Z",
     "shell.execute_reply.started": "2022-04-24T02:26:26.236198Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>a</th>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>b</th>\n",
       "      <td>4</td>\n",
       "      <td>5</td>\n",
       "      <td>6</td>\n",
       "      <td>7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>d</th>\n",
       "      <td>12</td>\n",
       "      <td>13</td>\n",
       "      <td>14</td>\n",
       "      <td>15</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    A   B   C   D\n",
       "a   0   1   2   3\n",
       "b   4   5   6   7\n",
       "d  12  13  14  15"
      ]
     },
     "execution_count": 60,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 用drop函数删除多行数据\n",
    "df.drop([\"c\", \"f\"])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "id": "a97cfd10-7dda-4572-80b9-28c4150ffb98",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-24T02:33:26.942768Z",
     "iopub.status.busy": "2022-04-24T02:33:26.942503Z",
     "iopub.status.idle": "2022-04-24T02:33:26.950600Z",
     "shell.execute_reply": "2022-04-24T02:33:26.949898Z",
     "shell.execute_reply.started": "2022-04-24T02:33:26.942742Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>a</th>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>b</th>\n",
       "      <td>5</td>\n",
       "      <td>6</td>\n",
       "      <td>7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>c</th>\n",
       "      <td>9</td>\n",
       "      <td>10</td>\n",
       "      <td>11</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>d</th>\n",
       "      <td>13</td>\n",
       "      <td>14</td>\n",
       "      <td>15</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>f</th>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    B   C   D\n",
       "a   1   2   3\n",
       "b   5   6   7\n",
       "c   9  10  11\n",
       "d  13  14  15\n",
       "f   2   2   2"
      ]
     },
     "execution_count": 61,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 用drop函数删除A索引的列数据\n",
    "df.drop(\"A\", axis=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "id": "0a8d258b-16a4-47ae-97eb-7ef11406586d",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-24T02:41:30.653372Z",
     "iopub.status.busy": "2022-04-24T02:41:30.653133Z",
     "iopub.status.idle": "2022-04-24T02:41:30.660990Z",
     "shell.execute_reply": "2022-04-24T02:41:30.660328Z",
     "shell.execute_reply.started": "2022-04-24T02:41:30.653350Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>a</th>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>b</th>\n",
       "      <td>5</td>\n",
       "      <td>6</td>\n",
       "      <td>7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>c</th>\n",
       "      <td>9</td>\n",
       "      <td>10</td>\n",
       "      <td>11</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>d</th>\n",
       "      <td>13</td>\n",
       "      <td>14</td>\n",
       "      <td>15</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>f</th>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    B   C   D\n",
       "a   1   2   3\n",
       "b   5   6   7\n",
       "c   9  10  11\n",
       "d  13  14  15\n",
       "f   2   2   2"
      ]
     },
     "execution_count": 62,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 用drop函数删除A索引的列数据\n",
    "df.drop(\"A\", axis=\"columns\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "id": "4fb3e800-5a09-4145-b1bc-f4deddfbf1f8",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-24T02:45:57.399489Z",
     "iopub.status.busy": "2022-04-24T02:45:57.399238Z",
     "iopub.status.idle": "2022-04-24T02:45:57.407043Z",
     "shell.execute_reply": "2022-04-24T02:45:57.406254Z",
     "shell.execute_reply.started": "2022-04-24T02:45:57.399464Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>a</th>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>b</th>\n",
       "      <td>4</td>\n",
       "      <td>5</td>\n",
       "      <td>6</td>\n",
       "      <td>7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>c</th>\n",
       "      <td>8</td>\n",
       "      <td>9</td>\n",
       "      <td>10</td>\n",
       "      <td>11</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>d</th>\n",
       "      <td>12</td>\n",
       "      <td>13</td>\n",
       "      <td>14</td>\n",
       "      <td>15</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>f</th>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    A   B   C   D\n",
       "a   0   1   2   3\n",
       "b   4   5   6   7\n",
       "c   8   9  10  11\n",
       "d  12  13  14  15\n",
       "f   2   2   2   2"
      ]
     },
     "execution_count": 63,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 查看df\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "id": "d1a3cde6-14e6-447e-8bdf-71f4f90617f4",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-24T02:47:50.597480Z",
     "iopub.status.busy": "2022-04-24T02:47:50.597223Z",
     "iopub.status.idle": "2022-04-24T02:47:50.605643Z",
     "shell.execute_reply": "2022-04-24T02:47:50.604963Z",
     "shell.execute_reply.started": "2022-04-24T02:47:50.597455Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>a</th>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>b</th>\n",
       "      <td>4</td>\n",
       "      <td>5</td>\n",
       "      <td>6</td>\n",
       "      <td>7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>c</th>\n",
       "      <td>8</td>\n",
       "      <td>9</td>\n",
       "      <td>10</td>\n",
       "      <td>11</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>d</th>\n",
       "      <td>12</td>\n",
       "      <td>13</td>\n",
       "      <td>14</td>\n",
       "      <td>15</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    A   B   C   D\n",
       "a   0   1   2   3\n",
       "b   4   5   6   7\n",
       "c   8   9  10  11\n",
       "d  12  13  14  15"
      ]
     },
     "execution_count": 64,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 使用drop函数在原对象上进行操作\n",
    "# 设置inplace的值为True\n",
    "df.drop(\"f\", inplace=True)\n",
    "df"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2362b9e5-6a86-43b4-bca2-bac9cb887784",
   "metadata": {
    "tags": []
   },
   "source": [
    "### 3.3 改"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "47915b77-26fb-4cb6-8eaf-a076d7f36004",
   "metadata": {},
   "source": [
    "#### 3.3.1 Series修改"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "id": "e73ea25e-6e0d-4afc-b91f-e9605d7f61ec",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-24T03:53:17.266594Z",
     "iopub.status.busy": "2022-04-24T03:53:17.266170Z",
     "iopub.status.idle": "2022-04-24T03:53:17.272079Z",
     "shell.execute_reply": "2022-04-24T03:53:17.271441Z",
     "shell.execute_reply.started": "2022-04-24T03:53:17.266566Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "b    1\n",
       "c    2\n",
       "d    3\n",
       "e    4\n",
       "g    8\n",
       "dtype: int64"
      ]
     },
     "execution_count": 66,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 查看pds\n",
    "pds"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "id": "f6efd245-c249-4741-b26b-da48f9ad47d1",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-24T03:55:47.693266Z",
     "iopub.status.busy": "2022-04-24T03:55:47.692980Z",
     "iopub.status.idle": "2022-04-24T03:55:47.698995Z",
     "shell.execute_reply": "2022-04-24T03:55:47.698363Z",
     "shell.execute_reply.started": "2022-04-24T03:55:47.693238Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "b    777\n",
       "c      2\n",
       "d      3\n",
       "e      4\n",
       "g      8\n",
       "dtype: int64"
      ]
     },
     "execution_count": 67,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 修改b标签对应的元素值为777\n",
    "pds[\"b\"] = 777\n",
    "pds"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "id": "08ae67ef-5b79-4631-a05c-af561cb747da",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-24T03:59:26.849141Z",
     "iopub.status.busy": "2022-04-24T03:59:26.848884Z",
     "iopub.status.idle": "2022-04-24T03:59:26.855267Z",
     "shell.execute_reply": "2022-04-24T03:59:26.854657Z",
     "shell.execute_reply.started": "2022-04-24T03:59:26.849116Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "b    1\n",
       "c    2\n",
       "d    3\n",
       "e    4\n",
       "g    8\n",
       "dtype: int64"
      ]
     },
     "execution_count": 68,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 修改索引下标修改对应的元素值\n",
    "pds[0] = 1\n",
    "pds"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "38e4bdab-35a0-4ea4-994f-23bdd3040e59",
   "metadata": {},
   "source": [
    "#### 3.3.2 DataFrame修改"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cc819624-94ee-49bc-9ac8-f7f6af8d7a6e",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-24T07:30:20.388238Z",
     "iopub.status.busy": "2022-04-24T07:30:20.387920Z",
     "iopub.status.idle": "2022-04-24T07:30:20.391643Z",
     "shell.execute_reply": "2022-04-24T07:30:20.390800Z",
     "shell.execute_reply.started": "2022-04-24T07:30:20.388189Z"
    },
    "tags": []
   },
   "source": [
    "##### 3.3.2.1 列修改"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "id": "f7782738-f276-4510-aa47-6f77d58b4e13",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-24T07:11:45.122815Z",
     "iopub.status.busy": "2022-04-24T07:11:45.122568Z",
     "iopub.status.idle": "2022-04-24T07:11:45.130569Z",
     "shell.execute_reply": "2022-04-24T07:11:45.129920Z",
     "shell.execute_reply.started": "2022-04-24T07:11:45.122789Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>a</th>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>b</th>\n",
       "      <td>4</td>\n",
       "      <td>5</td>\n",
       "      <td>6</td>\n",
       "      <td>7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>c</th>\n",
       "      <td>8</td>\n",
       "      <td>9</td>\n",
       "      <td>10</td>\n",
       "      <td>11</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>d</th>\n",
       "      <td>12</td>\n",
       "      <td>13</td>\n",
       "      <td>14</td>\n",
       "      <td>15</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    A   B   C   D\n",
       "a   0   1   2   3\n",
       "b   4   5   6   7\n",
       "c   8   9  10  11\n",
       "d  12  13  14  15"
      ]
     },
     "execution_count": 69,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 查看df\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "id": "7066c5d6-33d9-45a2-9a70-3a507e1d53cd",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-24T07:17:02.909107Z",
     "iopub.status.busy": "2022-04-24T07:17:02.908641Z",
     "iopub.status.idle": "2022-04-24T07:17:02.917069Z",
     "shell.execute_reply": "2022-04-24T07:17:02.916487Z",
     "shell.execute_reply.started": "2022-04-24T07:17:02.909069Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>a</th>\n",
       "      <td>999</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>b</th>\n",
       "      <td>999</td>\n",
       "      <td>5</td>\n",
       "      <td>6</td>\n",
       "      <td>7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>c</th>\n",
       "      <td>999</td>\n",
       "      <td>9</td>\n",
       "      <td>10</td>\n",
       "      <td>11</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>d</th>\n",
       "      <td>999</td>\n",
       "      <td>13</td>\n",
       "      <td>14</td>\n",
       "      <td>15</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "     A   B   C   D\n",
       "a  999   1   2   3\n",
       "b  999   5   6   7\n",
       "c  999   9  10  11\n",
       "d  999  13  14  15"
      ]
     },
     "execution_count": 70,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 把df中A索引的列都改成999\n",
    "df[\"A\"] = 999\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "id": "d1d2f099-7371-491c-8ee7-7a64c8d4332d",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-24T07:20:48.177814Z",
     "iopub.status.busy": "2022-04-24T07:20:48.177519Z",
     "iopub.status.idle": "2022-04-24T07:20:48.185763Z",
     "shell.execute_reply": "2022-04-24T07:20:48.185117Z",
     "shell.execute_reply.started": "2022-04-24T07:20:48.177789Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>a</th>\n",
       "      <td>5</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>b</th>\n",
       "      <td>6</td>\n",
       "      <td>5</td>\n",
       "      <td>6</td>\n",
       "      <td>7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>c</th>\n",
       "      <td>7</td>\n",
       "      <td>9</td>\n",
       "      <td>10</td>\n",
       "      <td>11</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>d</th>\n",
       "      <td>8</td>\n",
       "      <td>13</td>\n",
       "      <td>14</td>\n",
       "      <td>15</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   A   B   C   D\n",
       "a  5   1   2   3\n",
       "b  6   5   6   7\n",
       "c  7   9  10  11\n",
       "d  8  13  14  15"
      ]
     },
     "execution_count": 71,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 把df中A索引的列都改成[5, 6, 7, 8]\n",
    "df[\"A\"] = [5, 6, 7, 8]\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "id": "cbca019c-9bb9-43ec-aecb-dc4acb454c42",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-24T07:26:15.685755Z",
     "iopub.status.busy": "2022-04-24T07:26:15.685496Z",
     "iopub.status.idle": "2022-04-24T07:26:15.694035Z",
     "shell.execute_reply": "2022-04-24T07:26:15.693214Z",
     "shell.execute_reply.started": "2022-04-24T07:26:15.685726Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>a</th>\n",
       "      <td>666</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>b</th>\n",
       "      <td>666</td>\n",
       "      <td>5</td>\n",
       "      <td>6</td>\n",
       "      <td>7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>c</th>\n",
       "      <td>666</td>\n",
       "      <td>9</td>\n",
       "      <td>10</td>\n",
       "      <td>11</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>d</th>\n",
       "      <td>666</td>\n",
       "      <td>13</td>\n",
       "      <td>14</td>\n",
       "      <td>15</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "     A   B   C   D\n",
       "a  666   1   2   3\n",
       "b  666   5   6   7\n",
       "c  666   9  10  11\n",
       "d  666  13  14  15"
      ]
     },
     "execution_count": 72,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 把df中A索引的列都改成666，“对象.列”形式修改\n",
    "df.A = 666\n",
    "df"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "39c4360f-350f-4085-89c8-4b09d8c18221",
   "metadata": {},
   "source": [
    "##### 3.3.2.2 行修改"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 74,
   "id": "320992a6-650f-4dda-867e-4a9f33b72590",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-24T07:33:28.077895Z",
     "iopub.status.busy": "2022-04-24T07:33:28.077531Z",
     "iopub.status.idle": "2022-04-24T07:33:28.085745Z",
     "shell.execute_reply": "2022-04-24T07:33:28.085153Z",
     "shell.execute_reply.started": "2022-04-24T07:33:28.077869Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "      <th>a</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>a</th>\n",
       "      <td>666</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "      <td>777</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>b</th>\n",
       "      <td>666</td>\n",
       "      <td>5</td>\n",
       "      <td>6</td>\n",
       "      <td>7</td>\n",
       "      <td>777</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>c</th>\n",
       "      <td>666</td>\n",
       "      <td>9</td>\n",
       "      <td>10</td>\n",
       "      <td>11</td>\n",
       "      <td>777</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>d</th>\n",
       "      <td>666</td>\n",
       "      <td>13</td>\n",
       "      <td>14</td>\n",
       "      <td>15</td>\n",
       "      <td>777</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "     A   B   C   D    a\n",
       "a  666   1   2   3  777\n",
       "b  666   5   6   7  777\n",
       "c  666   9  10  11  777\n",
       "d  666  13  14  15  777"
      ]
     },
     "execution_count": 74,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 修改a索引的行为777？\n",
    "df[\"a\"] = 777\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 83,
   "id": "a5ec8516-c173-4339-b5dc-fac370434bfa",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-24T07:43:40.579658Z",
     "iopub.status.busy": "2022-04-24T07:43:40.579413Z",
     "iopub.status.idle": "2022-04-24T07:43:40.587312Z",
     "shell.execute_reply": "2022-04-24T07:43:40.586589Z",
     "shell.execute_reply.started": "2022-04-24T07:43:40.579633Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "      <th>a</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>a</th>\n",
       "      <td>777</td>\n",
       "      <td>777</td>\n",
       "      <td>777</td>\n",
       "      <td>777</td>\n",
       "      <td>777</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>b</th>\n",
       "      <td>666</td>\n",
       "      <td>5</td>\n",
       "      <td>6</td>\n",
       "      <td>7</td>\n",
       "      <td>777</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>c</th>\n",
       "      <td>666</td>\n",
       "      <td>9</td>\n",
       "      <td>10</td>\n",
       "      <td>11</td>\n",
       "      <td>777</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>d</th>\n",
       "      <td>666</td>\n",
       "      <td>13</td>\n",
       "      <td>14</td>\n",
       "      <td>15</td>\n",
       "      <td>777</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "     A    B    C    D    a\n",
       "a  777  777  777  777  777\n",
       "b  666    5    6    7  777\n",
       "c  666    9   10   11  777\n",
       "d  666   13   14   15  777"
      ]
     },
     "execution_count": 83,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 修改a索引的行为777，使用loc索引\n",
    "df.loc[\"a\"] = 777\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 85,
   "id": "547d5dc6-ecab-41a4-a75e-e5fb9c99ac73",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-24T07:46:36.505048Z",
     "iopub.status.busy": "2022-04-24T07:46:36.504657Z",
     "iopub.status.idle": "2022-04-24T07:46:36.513221Z",
     "shell.execute_reply": "2022-04-24T07:46:36.512324Z",
     "shell.execute_reply.started": "2022-04-24T07:46:36.505021Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "      <th>a</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>a</th>\n",
       "      <td>9999</td>\n",
       "      <td>777</td>\n",
       "      <td>777</td>\n",
       "      <td>777</td>\n",
       "      <td>777</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>b</th>\n",
       "      <td>666</td>\n",
       "      <td>5</td>\n",
       "      <td>6</td>\n",
       "      <td>7</td>\n",
       "      <td>777</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>c</th>\n",
       "      <td>666</td>\n",
       "      <td>9</td>\n",
       "      <td>10</td>\n",
       "      <td>11</td>\n",
       "      <td>777</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>d</th>\n",
       "      <td>666</td>\n",
       "      <td>13</td>\n",
       "      <td>14</td>\n",
       "      <td>15</td>\n",
       "      <td>777</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "      A    B    C    D    a\n",
       "a  9999  777  777  777  777\n",
       "b   666    5    6    7  777\n",
       "c   666    9   10   11  777\n",
       "d   666   13   14   15  777"
      ]
     },
     "execution_count": 85,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 把a行A列的数据修改为9999\n",
    "df.loc[\"a\", \"A\"] = 9999\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 87,
   "id": "ba25ec56-39da-498e-850f-476e4b67abb9",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-24T07:49:10.653161Z",
     "iopub.status.busy": "2022-04-24T07:49:10.652912Z",
     "iopub.status.idle": "2022-04-24T07:49:10.659087Z",
     "shell.execute_reply": "2022-04-24T07:49:10.658490Z",
     "shell.execute_reply.started": "2022-04-24T07:49:10.653137Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "a    9999\n",
       "b     666\n",
       "c     666\n",
       "d     666\n",
       "Name: A, dtype: int64"
      ]
     },
     "execution_count": 87,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.loc[:, \"A\"]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d7c8c314-65ab-4d54-9ff6-3fd3bf561e87",
   "metadata": {},
   "source": [
    "### 3.4 查"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ed99061b-2108-4570-8179-195cddf36339",
   "metadata": {},
   "source": [
    "#### 3.4.1 Series查询"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f92a6026-d545-4209-8743-97ce1568b958",
   "metadata": {},
   "source": [
    "##### 3.4.1.1 单个索引查询"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 88,
   "id": "b3026027-a48c-4fb4-b905-59cf8a702114",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-24T08:42:42.832186Z",
     "iopub.status.busy": "2022-04-24T08:42:42.831939Z",
     "iopub.status.idle": "2022-04-24T08:42:42.837241Z",
     "shell.execute_reply": "2022-04-24T08:42:42.836515Z",
     "shell.execute_reply.started": "2022-04-24T08:42:42.832159Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "b    1\n",
       "c    2\n",
       "d    3\n",
       "e    4\n",
       "g    8\n",
       "dtype: int64"
      ]
     },
     "execution_count": 88,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 查看pds\n",
    "pds"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 89,
   "id": "da03e4f9-60ed-4568-8f9e-6f754d1113c2",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-24T08:44:43.085596Z",
     "iopub.status.busy": "2022-04-24T08:44:43.085348Z",
     "iopub.status.idle": "2022-04-24T08:44:43.089953Z",
     "shell.execute_reply": "2022-04-24T08:44:43.089322Z",
     "shell.execute_reply.started": "2022-04-24T08:44:43.085569Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "8"
      ]
     },
     "execution_count": 89,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 查看g索引的行，使用标签索引\n",
    "pds[\"g\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 90,
   "id": "32e6c508-0fb5-44e3-a6b3-02381dc1323f",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-24T08:47:27.082323Z",
     "iopub.status.busy": "2022-04-24T08:47:27.081700Z",
     "iopub.status.idle": "2022-04-24T08:47:27.087820Z",
     "shell.execute_reply": "2022-04-24T08:47:27.087206Z",
     "shell.execute_reply.started": "2022-04-24T08:47:27.082294Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "8"
      ]
     },
     "execution_count": 90,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 查看g索引的行，使用位置索引\n",
    "pds[4]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "47d9119d-4f5f-48cd-84ce-ab8e790d1613",
   "metadata": {},
   "source": [
    "##### 3.4.1.2 多个索引查询-切片"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 93,
   "id": "7d4b0ed5-029c-491b-acd0-e55cdea17045",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-24T08:58:42.463680Z",
     "iopub.status.busy": "2022-04-24T08:58:42.463298Z",
     "iopub.status.idle": "2022-04-24T08:58:42.469465Z",
     "shell.execute_reply": "2022-04-24T08:58:42.468862Z",
     "shell.execute_reply.started": "2022-04-24T08:58:42.463639Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "c    2\n",
       "d    3\n",
       "e    4\n",
       "dtype: int64"
      ]
     },
     "execution_count": 93,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 切片，开始位置1，结束位置4\n",
    "pds[1:4]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 94,
   "id": "ae79e94c-1fe4-441c-91b9-a1433dbaf393",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-24T08:59:13.205529Z",
     "iopub.status.busy": "2022-04-24T08:59:13.205222Z",
     "iopub.status.idle": "2022-04-24T08:59:13.213452Z",
     "shell.execute_reply": "2022-04-24T08:59:13.212825Z",
     "shell.execute_reply.started": "2022-04-24T08:59:13.205503Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "c    2\n",
       "d    3\n",
       "e    4\n",
       "g    8\n",
       "dtype: int64"
      ]
     },
     "execution_count": 94,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 切片，开始位置c，结束位置g\n",
    "pds[\"c\":\"g\"]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d268e5e5-f56b-4725-b2f0-6e32f0c06539",
   "metadata": {},
   "source": [
    "##### 3.4.1.3 多个索引查询-不连续索引"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 95,
   "id": "2201fe6c-ceb9-4b30-b8e7-16aab1ecb1d7",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-24T09:14:01.958700Z",
     "iopub.status.busy": "2022-04-24T09:14:01.958377Z",
     "iopub.status.idle": "2022-04-24T09:14:01.964689Z",
     "shell.execute_reply": "2022-04-24T09:14:01.963992Z",
     "shell.execute_reply.started": "2022-04-24T09:14:01.958672Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "g    8\n",
       "d    3\n",
       "dtype: int64"
      ]
     },
     "execution_count": 95,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 不连续索引，去第4行和第2行\n",
    "pds[[4, 2]]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 92,
   "id": "8701e478-fcf4-4d72-bbce-96781c0cd835",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-24T08:53:37.093217Z",
     "iopub.status.busy": "2022-04-24T08:53:37.092853Z",
     "iopub.status.idle": "2022-04-24T08:53:37.099240Z",
     "shell.execute_reply": "2022-04-24T08:53:37.098647Z",
     "shell.execute_reply.started": "2022-04-24T08:53:37.093190Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "g    8\n",
       "d    3\n",
       "dtype: int64"
      ]
     },
     "execution_count": 92,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 不连续索引，去g行和d行\n",
    "pds[[\"g\", \"d\"]]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "65d7b3e3-4cf6-42ae-b5f1-c8c5840967c0",
   "metadata": {},
   "source": [
    "##### 3.4.1.4 布尔索引查询"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 96,
   "id": "d4b2682c-b7cc-4f5b-a6cc-b98c9bfc7248",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-24T17:49:20.627818Z",
     "iopub.status.busy": "2022-04-24T17:49:20.627546Z",
     "iopub.status.idle": "2022-04-24T17:49:20.634392Z",
     "shell.execute_reply": "2022-04-24T17:49:20.633358Z",
     "shell.execute_reply.started": "2022-04-24T17:49:20.627793Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "d    3\n",
       "e    4\n",
       "g    8\n",
       "dtype: int64"
      ]
     },
     "execution_count": 96,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 查询pds里大于2的数据\n",
    "pds[pds > 2]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "904e1de6-9f72-4b6d-8912-06482d8a7ed8",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-24T17:52:56.599335Z",
     "iopub.status.busy": "2022-04-24T17:52:56.599094Z",
     "iopub.status.idle": "2022-04-24T17:52:56.604740Z",
     "shell.execute_reply": "2022-04-24T17:52:56.604002Z",
     "shell.execute_reply.started": "2022-04-24T17:52:56.599312Z"
    },
    "tags": []
   },
   "source": [
    "#### 3.4.2 DataFrame查询"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 98,
   "id": "30a078d7-0dd9-4a65-9bfd-d369ca99f373",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-24T17:57:41.784407Z",
     "iopub.status.busy": "2022-04-24T17:57:41.783856Z",
     "iopub.status.idle": "2022-04-24T17:57:41.791444Z",
     "shell.execute_reply": "2022-04-24T17:57:41.790766Z",
     "shell.execute_reply.started": "2022-04-24T17:57:41.784388Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "      <th>a</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>a</th>\n",
       "      <td>9999</td>\n",
       "      <td>777</td>\n",
       "      <td>777</td>\n",
       "      <td>777</td>\n",
       "      <td>777</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>b</th>\n",
       "      <td>666</td>\n",
       "      <td>5</td>\n",
       "      <td>6</td>\n",
       "      <td>7</td>\n",
       "      <td>777</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>c</th>\n",
       "      <td>666</td>\n",
       "      <td>9</td>\n",
       "      <td>10</td>\n",
       "      <td>11</td>\n",
       "      <td>777</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>d</th>\n",
       "      <td>666</td>\n",
       "      <td>13</td>\n",
       "      <td>14</td>\n",
       "      <td>15</td>\n",
       "      <td>777</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "      A    B    C    D    a\n",
       "a  9999  777  777  777  777\n",
       "b   666    5    6    7  777\n",
       "c   666    9   10   11  777\n",
       "d   666   13   14   15  777"
      ]
     },
     "execution_count": 98,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 查看df\n",
    "df"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e298ae7b-41c4-482c-b557-00e9347388ae",
   "metadata": {},
   "source": [
    "##### 3.4.2.1 查看单列索引"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 100,
   "id": "0244a07b-bc04-412c-bd11-1a28fbbe6b66",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-24T18:02:14.933176Z",
     "iopub.status.busy": "2022-04-24T18:02:14.932923Z",
     "iopub.status.idle": "2022-04-24T18:02:14.938387Z",
     "shell.execute_reply": "2022-04-24T18:02:14.937774Z",
     "shell.execute_reply.started": "2022-04-24T18:02:14.933150Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "a    9999\n",
       "b     666\n",
       "c     666\n",
       "d     666\n",
       "Name: A, dtype: int64"
      ]
     },
     "execution_count": 100,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 查看A列的数据，使用标签索引\n",
    "df[\"A\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 101,
   "id": "d727b333-7ab3-464d-91dd-0b0537c57b48",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-24T18:06:19.366672Z",
     "iopub.status.busy": "2022-04-24T18:06:19.366424Z",
     "iopub.status.idle": "2022-04-24T18:06:19.493878Z",
     "shell.execute_reply": "2022-04-24T18:06:19.492936Z",
     "shell.execute_reply.started": "2022-04-24T18:06:19.366647Z"
    }
   },
   "outputs": [
    {
     "ename": "KeyError",
     "evalue": "0",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mKeyError\u001b[0m                                  Traceback (most recent call last)",
      "File \u001b[0;32m/opt/miniconda3/envs/jpt/lib/python3.10/site-packages/pandas/core/indexes/base.py:3621\u001b[0m, in \u001b[0;36mIndex.get_loc\u001b[0;34m(self, key, method, tolerance)\u001b[0m\n\u001b[1;32m   3620\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[0;32m-> 3621\u001b[0m     \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_engine\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mget_loc\u001b[49m\u001b[43m(\u001b[49m\u001b[43mcasted_key\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m   3622\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mKeyError\u001b[39;00m \u001b[38;5;28;01mas\u001b[39;00m err:\n",
      "File \u001b[0;32m/opt/miniconda3/envs/jpt/lib/python3.10/site-packages/pandas/_libs/index.pyx:136\u001b[0m, in \u001b[0;36mpandas._libs.index.IndexEngine.get_loc\u001b[0;34m()\u001b[0m\n",
      "File \u001b[0;32m/opt/miniconda3/envs/jpt/lib/python3.10/site-packages/pandas/_libs/index.pyx:163\u001b[0m, in \u001b[0;36mpandas._libs.index.IndexEngine.get_loc\u001b[0;34m()\u001b[0m\n",
      "File \u001b[0;32mpandas/_libs/hashtable_class_helper.pxi:5198\u001b[0m, in \u001b[0;36mpandas._libs.hashtable.PyObjectHashTable.get_item\u001b[0;34m()\u001b[0m\n",
      "File \u001b[0;32mpandas/_libs/hashtable_class_helper.pxi:5206\u001b[0m, in \u001b[0;36mpandas._libs.hashtable.PyObjectHashTable.get_item\u001b[0;34m()\u001b[0m\n",
      "\u001b[0;31mKeyError\u001b[0m: 0",
      "\nThe above exception was the direct cause of the following exception:\n",
      "\u001b[0;31mKeyError\u001b[0m                                  Traceback (most recent call last)",
      "Input \u001b[0;32mIn [101]\u001b[0m, in \u001b[0;36m<cell line: 2>\u001b[0;34m()\u001b[0m\n\u001b[1;32m      1\u001b[0m \u001b[38;5;66;03m# 查看A列的数据，使用位置索引\u001b[39;00m\n\u001b[0;32m----> 2\u001b[0m \u001b[43mdf\u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;241;43m0\u001b[39;49m\u001b[43m]\u001b[49m\n",
      "File \u001b[0;32m/opt/miniconda3/envs/jpt/lib/python3.10/site-packages/pandas/core/frame.py:3505\u001b[0m, in \u001b[0;36mDataFrame.__getitem__\u001b[0;34m(self, key)\u001b[0m\n\u001b[1;32m   3503\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mcolumns\u001b[38;5;241m.\u001b[39mnlevels \u001b[38;5;241m>\u001b[39m \u001b[38;5;241m1\u001b[39m:\n\u001b[1;32m   3504\u001b[0m     \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_getitem_multilevel(key)\n\u001b[0;32m-> 3505\u001b[0m indexer \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mcolumns\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mget_loc\u001b[49m\u001b[43m(\u001b[49m\u001b[43mkey\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m   3506\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m is_integer(indexer):\n\u001b[1;32m   3507\u001b[0m     indexer \u001b[38;5;241m=\u001b[39m [indexer]\n",
      "File \u001b[0;32m/opt/miniconda3/envs/jpt/lib/python3.10/site-packages/pandas/core/indexes/base.py:3623\u001b[0m, in \u001b[0;36mIndex.get_loc\u001b[0;34m(self, key, method, tolerance)\u001b[0m\n\u001b[1;32m   3621\u001b[0m     \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_engine\u001b[38;5;241m.\u001b[39mget_loc(casted_key)\n\u001b[1;32m   3622\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mKeyError\u001b[39;00m \u001b[38;5;28;01mas\u001b[39;00m err:\n\u001b[0;32m-> 3623\u001b[0m     \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mKeyError\u001b[39;00m(key) \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01merr\u001b[39;00m\n\u001b[1;32m   3624\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mTypeError\u001b[39;00m:\n\u001b[1;32m   3625\u001b[0m     \u001b[38;5;66;03m# If we have a listlike key, _check_indexing_error will raise\u001b[39;00m\n\u001b[1;32m   3626\u001b[0m     \u001b[38;5;66;03m#  InvalidIndexError. Otherwise we fall through and re-raise\u001b[39;00m\n\u001b[1;32m   3627\u001b[0m     \u001b[38;5;66;03m#  the TypeError.\u001b[39;00m\n\u001b[1;32m   3628\u001b[0m     \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_check_indexing_error(key)\n",
      "\u001b[0;31mKeyError\u001b[0m: 0"
     ]
    }
   ],
   "source": [
    "# 查看A列的数据，使用位置索引\n",
    "# DataFrame不能这么操作\n",
    "df[0]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5b2d5b34-87bd-48e2-a7a4-6e46a6905dae",
   "metadata": {},
   "source": [
    "##### 3.4.2.2 查看多列索引"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 107,
   "id": "5b96d744-7cc3-4181-9c7d-88c3bade841a",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-24T18:13:47.719478Z",
     "iopub.status.busy": "2022-04-24T18:13:47.719254Z",
     "iopub.status.idle": "2022-04-24T18:13:47.726479Z",
     "shell.execute_reply": "2022-04-24T18:13:47.725807Z",
     "shell.execute_reply.started": "2022-04-24T18:13:47.719457Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>D</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>a</th>\n",
       "      <td>9999</td>\n",
       "      <td>777</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>b</th>\n",
       "      <td>666</td>\n",
       "      <td>7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>c</th>\n",
       "      <td>666</td>\n",
       "      <td>11</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>d</th>\n",
       "      <td>666</td>\n",
       "      <td>15</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "      A    D\n",
       "a  9999  777\n",
       "b   666    7\n",
       "c   666   11\n",
       "d   666   15"
      ]
     },
     "execution_count": 107,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 不连续列索引，取A列和D列\n",
    "df[[\"A\", \"D\"]]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "113e0866-fe6c-447b-9749-486c70fbcdc8",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-24T18:18:54.936422Z",
     "iopub.status.busy": "2022-04-24T18:18:54.936138Z",
     "iopub.status.idle": "2022-04-24T18:18:55.042739Z",
     "shell.execute_reply": "2022-04-24T18:18:55.041846Z",
     "shell.execute_reply.started": "2022-04-24T18:18:54.936392Z"
    },
    "tags": []
   },
   "source": [
    "##### 3.4.2.3 查看单个数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 117,
   "id": "cb83af68-42c6-468a-9132-394f537c6b3a",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-24T18:24:58.245652Z",
     "iopub.status.busy": "2022-04-24T18:24:58.245384Z",
     "iopub.status.idle": "2022-04-24T18:24:58.250479Z",
     "shell.execute_reply": "2022-04-24T18:24:58.249886Z",
     "shell.execute_reply.started": "2022-04-24T18:24:58.245625Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "9999"
      ]
     },
     "execution_count": 117,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 查看单个元素，A列a行\n",
    "df[\"A\"][\"a\"]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7f2d3a1a-0750-4a1f-a851-079f9bdfdb45",
   "metadata": {},
   "source": [
    "##### 3.4.2.4 切片"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 120,
   "id": "587b7ea7-be4e-4968-895b-c091a79eef6c",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-24T18:32:27.133665Z",
     "iopub.status.busy": "2022-04-24T18:32:27.133402Z",
     "iopub.status.idle": "2022-04-24T18:32:27.142142Z",
     "shell.execute_reply": "2022-04-24T18:32:27.141305Z",
     "shell.execute_reply.started": "2022-04-24T18:32:27.133638Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "      <th>a</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>a</th>\n",
       "      <td>9999</td>\n",
       "      <td>777</td>\n",
       "      <td>777</td>\n",
       "      <td>777</td>\n",
       "      <td>777</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>b</th>\n",
       "      <td>666</td>\n",
       "      <td>5</td>\n",
       "      <td>6</td>\n",
       "      <td>7</td>\n",
       "      <td>777</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>c</th>\n",
       "      <td>666</td>\n",
       "      <td>9</td>\n",
       "      <td>10</td>\n",
       "      <td>11</td>\n",
       "      <td>777</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "      A    B    C    D    a\n",
       "a  9999  777  777  777  777\n",
       "b   666    5    6    7  777\n",
       "c   666    9   10   11  777"
      ]
     },
     "execution_count": 120,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 切片a行到c行，标签索引\n",
    "df[\"a\":\"c\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 121,
   "id": "5c30bf9f-3903-4e37-87c1-5c716628a319",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-24T18:33:22.180327Z",
     "iopub.status.busy": "2022-04-24T18:33:22.180014Z",
     "iopub.status.idle": "2022-04-24T18:33:22.187795Z",
     "shell.execute_reply": "2022-04-24T18:33:22.187097Z",
     "shell.execute_reply.started": "2022-04-24T18:33:22.180303Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "      <th>a</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>a</th>\n",
       "      <td>9999</td>\n",
       "      <td>777</td>\n",
       "      <td>777</td>\n",
       "      <td>777</td>\n",
       "      <td>777</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>b</th>\n",
       "      <td>666</td>\n",
       "      <td>5</td>\n",
       "      <td>6</td>\n",
       "      <td>7</td>\n",
       "      <td>777</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "      A    B    C    D    a\n",
       "a  9999  777  777  777  777\n",
       "b   666    5    6    7  777"
      ]
     },
     "execution_count": 121,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 切片0行到2行，位置索引\n",
    "df[0:2]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "52dd3339-88e5-4f2d-b9b1-bafb3a87b5d8",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-24T18:41:32.318740Z",
     "iopub.status.busy": "2022-04-24T18:41:32.318492Z",
     "iopub.status.idle": "2022-04-24T18:41:32.322208Z",
     "shell.execute_reply": "2022-04-24T18:41:32.321235Z",
     "shell.execute_reply.started": "2022-04-24T18:41:32.318716Z"
    }
   },
   "source": [
    "## 四、高级索引"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f96a6a03-bbed-442a-96ac-a687b58b435a",
   "metadata": {},
   "source": [
    "### 小结\n",
    "1. loc索引:  标签索引\n",
    "    1. 只接受自定义标签\n",
    "    2. 可以对行切片，也可以对列切片\n",
    "    3. 取stop位置\n",
    "2. iloc索引:  位置索引\n",
    "    1. 只接受位置标签\n",
    "    2. 可以对行切片，也可以对列切片\n",
    "    3. 不取stop位置"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "61d19f49-feab-4ab8-b946-52dc7b66f3d3",
   "metadata": {},
   "source": [
    "### 4.1 loc 标签索引"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 123,
   "id": "322b22ba-c53a-4d2b-bf34-039125157dd7",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-24T19:34:29.130779Z",
     "iopub.status.busy": "2022-04-24T19:34:29.130494Z",
     "iopub.status.idle": "2022-04-24T19:34:29.135972Z",
     "shell.execute_reply": "2022-04-24T19:34:29.135295Z",
     "shell.execute_reply.started": "2022-04-24T19:34:29.130754Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "b    1\n",
       "c    2\n",
       "d    3\n",
       "e    4\n",
       "g    8\n",
       "dtype: int64"
      ]
     },
     "execution_count": 123,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 查看pds\n",
    "pds"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 124,
   "id": "a28b99fa-890b-402b-95aa-f3aab9e234fa",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-24T19:35:43.338022Z",
     "iopub.status.busy": "2022-04-24T19:35:43.337663Z",
     "iopub.status.idle": "2022-04-24T19:35:43.343728Z",
     "shell.execute_reply": "2022-04-24T19:35:43.343111Z",
     "shell.execute_reply.started": "2022-04-24T19:35:43.337991Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "c    2\n",
       "d    3\n",
       "e    4\n",
       "g    8\n",
       "dtype: int64"
      ]
     },
     "execution_count": 124,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 切片，开始位置c，结束位置g，使用传统方式\n",
    "pds[\"c\":\"g\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 125,
   "id": "876abc5b-f1c7-4997-84fc-d586f59eeb78",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-24T19:38:41.509760Z",
     "iopub.status.busy": "2022-04-24T19:38:41.509484Z",
     "iopub.status.idle": "2022-04-24T19:38:41.516180Z",
     "shell.execute_reply": "2022-04-24T19:38:41.515209Z",
     "shell.execute_reply.started": "2022-04-24T19:38:41.509732Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "c    2\n",
       "d    3\n",
       "e    4\n",
       "g    8\n",
       "dtype: int64"
      ]
     },
     "execution_count": 125,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 切片，开始位置c，结束位置g，使用标签索引\n",
    "pds.loc[\"c\":\"g\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 126,
   "id": "17a4cb37-48fc-4717-9183-fde6aced4b2b",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-24T19:43:04.415765Z",
     "iopub.status.busy": "2022-04-24T19:43:04.415452Z",
     "iopub.status.idle": "2022-04-24T19:43:04.424246Z",
     "shell.execute_reply": "2022-04-24T19:43:04.423557Z",
     "shell.execute_reply.started": "2022-04-24T19:43:04.415741Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "      <th>a</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>a</th>\n",
       "      <td>9999</td>\n",
       "      <td>777</td>\n",
       "      <td>777</td>\n",
       "      <td>777</td>\n",
       "      <td>777</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>b</th>\n",
       "      <td>666</td>\n",
       "      <td>5</td>\n",
       "      <td>6</td>\n",
       "      <td>7</td>\n",
       "      <td>777</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>c</th>\n",
       "      <td>666</td>\n",
       "      <td>9</td>\n",
       "      <td>10</td>\n",
       "      <td>11</td>\n",
       "      <td>777</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>d</th>\n",
       "      <td>666</td>\n",
       "      <td>13</td>\n",
       "      <td>14</td>\n",
       "      <td>15</td>\n",
       "      <td>777</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "      A    B    C    D    a\n",
       "a  9999  777  777  777  777\n",
       "b   666    5    6    7  777\n",
       "c   666    9   10   11  777\n",
       "d   666   13   14   15  777"
      ]
     },
     "execution_count": 126,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 查看df\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 130,
   "id": "3c54ef0e-8f3f-48ec-854d-cdd472c02322",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-24T19:47:30.936858Z",
     "iopub.status.busy": "2022-04-24T19:47:30.936613Z",
     "iopub.status.idle": "2022-04-24T19:47:30.945453Z",
     "shell.execute_reply": "2022-04-24T19:47:30.944344Z",
     "shell.execute_reply.started": "2022-04-24T19:47:30.936832Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>a</th>\n",
       "      <td>9999</td>\n",
       "      <td>777</td>\n",
       "      <td>777</td>\n",
       "      <td>777</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>b</th>\n",
       "      <td>666</td>\n",
       "      <td>5</td>\n",
       "      <td>6</td>\n",
       "      <td>7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>c</th>\n",
       "      <td>666</td>\n",
       "      <td>9</td>\n",
       "      <td>10</td>\n",
       "      <td>11</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "      A    B    C    D\n",
       "a  9999  777  777  777\n",
       "b   666    5    6    7\n",
       "c   666    9   10   11"
      ]
     },
     "execution_count": 130,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 切片：a-c行，A-D列\n",
    "df.loc[\"a\":\"c\", \"A\":\"D\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 133,
   "id": "20fba452-02e8-41c7-9a44-87d22c259f4d",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-24T19:52:42.949531Z",
     "iopub.status.busy": "2022-04-24T19:52:42.949285Z",
     "iopub.status.idle": "2022-04-24T19:52:42.959504Z",
     "shell.execute_reply": "2022-04-24T19:52:42.958335Z",
     "shell.execute_reply.started": "2022-04-24T19:52:42.949505Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>D</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>a</th>\n",
       "      <td>9999</td>\n",
       "      <td>777</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>c</th>\n",
       "      <td>666</td>\n",
       "      <td>11</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "      A    D\n",
       "a  9999  777\n",
       "c   666   11"
      ]
     },
     "execution_count": 133,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 离散去值：a，c行，A，D列\n",
    "df.loc[[\"a\", \"c\"], [\"A\", \"D\"]]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "81d4f8c9-b7bf-44cc-b703-e7557a586c84",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-24T19:57:01.682884Z",
     "iopub.status.busy": "2022-04-24T19:57:01.682595Z",
     "iopub.status.idle": "2022-04-24T19:57:01.686428Z",
     "shell.execute_reply": "2022-04-24T19:57:01.685742Z",
     "shell.execute_reply.started": "2022-04-24T19:57:01.682836Z"
    }
   },
   "source": [
    "### 4.2 iloc 位置索引"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 135,
   "id": "9af99647-4fc3-4db4-8902-4b14a7defa01",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-24T19:59:00.502990Z",
     "iopub.status.busy": "2022-04-24T19:59:00.502746Z",
     "iopub.status.idle": "2022-04-24T19:59:00.509509Z",
     "shell.execute_reply": "2022-04-24T19:59:00.508598Z",
     "shell.execute_reply.started": "2022-04-24T19:59:00.502967Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "c    2\n",
       "d    3\n",
       "e    4\n",
       "dtype: int64"
      ]
     },
     "execution_count": 135,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 切片，开始位置1，结束位置4，使用传统方式\n",
    "pds[1:4]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 136,
   "id": "fa146c78-b494-4853-bad0-abbc010acb85",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-24T19:59:49.355009Z",
     "iopub.status.busy": "2022-04-24T19:59:49.354747Z",
     "iopub.status.idle": "2022-04-24T19:59:49.361264Z",
     "shell.execute_reply": "2022-04-24T19:59:49.360584Z",
     "shell.execute_reply.started": "2022-04-24T19:59:49.354983Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "c    2\n",
       "d    3\n",
       "e    4\n",
       "dtype: int64"
      ]
     },
     "execution_count": 136,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 切片，开始位置1，结束位置4，使用位置索引\n",
    "pds.iloc[1:4]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 138,
   "id": "d32ce8be-f5ce-4482-87ac-09628f2d024b",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-24T20:04:57.939434Z",
     "iopub.status.busy": "2022-04-24T20:04:57.939204Z",
     "iopub.status.idle": "2022-04-24T20:04:57.946120Z",
     "shell.execute_reply": "2022-04-24T20:04:57.945590Z",
     "shell.execute_reply.started": "2022-04-24T20:04:57.939410Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>a</th>\n",
       "      <td>9999</td>\n",
       "      <td>777</td>\n",
       "      <td>777</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>b</th>\n",
       "      <td>666</td>\n",
       "      <td>5</td>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>c</th>\n",
       "      <td>666</td>\n",
       "      <td>9</td>\n",
       "      <td>10</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "      A    B    C\n",
       "a  9999  777  777\n",
       "b   666    5    6\n",
       "c   666    9   10"
      ]
     },
     "execution_count": 138,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 切片：0-2行，0-3列\n",
    "df.iloc[0:3, 0:3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 140,
   "id": "bb0f6017-0e08-4086-977f-70e6de9bfa55",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-24T20:06:42.894924Z",
     "iopub.status.busy": "2022-04-24T20:06:42.894699Z",
     "iopub.status.idle": "2022-04-24T20:06:42.902664Z",
     "shell.execute_reply": "2022-04-24T20:06:42.902029Z",
     "shell.execute_reply.started": "2022-04-24T20:06:42.894902Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>D</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>a</th>\n",
       "      <td>9999</td>\n",
       "      <td>777</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>c</th>\n",
       "      <td>666</td>\n",
       "      <td>11</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "      A    D\n",
       "a  9999  777\n",
       "c   666   11"
      ]
     },
     "execution_count": 140,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 离散去值：0，2行，0，3列\n",
    "df.iloc[[0, 2], [0, 3]]"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
